package com.example.portal.colmapping.service;

import com.example.portal.colmapping.dao.ColMappingDao;
import com.example.portal.colmapping.model.ColMappingDO;
import com.example.util.GUID;
import com.example.util.PageResult;
import com.example.util.RowBoundUtil;
import com.example.util.StringUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class ColMappingServiceImpl {

    @Resource
    ColMappingDao colMappingDao;

    @Resource
    RowBoundUtil rowBoundUtil;

    /**
     * 分页查询
     * @param params
     * @return
     */

    public PageResult<List<ColMappingDO>> getMappings(Map<String, Object> params){
        String pageIndex = params.get("pageIndex") == null ? "" : params.get("pageIndex").toString();
        String pageSize = params.get("pageSize") == null ? "" : params.get("pageSize").toString();
        String key = params.get("key") == null ? "" : params.get("key").toString();
        List<ColMappingDO> colMappingDOS = new ArrayList<>();
        int count = 0;
        Example example = new Example(ColMappingDO.class);
        if(!StringUtil.isNullOrEmpty(key)){
            Example.Criteria criteria = example.createCriteria();
            criteria.andCondition(" excel_column like '%"+key+"%' OR  table_column like '%"+key +"'");
            count = colMappingDao.selectCountByExample(example);
        }else{
            count = colMappingDao.selectAll().size();
        }
        RowBounds rowBounds = rowBoundUtil.getRowBounds(pageSize, pageIndex);
        example.setOrderByClause(" table_id,table_column desc");
        colMappingDOS = colMappingDao.selectByExampleAndRowBounds(example,rowBounds);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setData(colMappingDOS);
        return pageResult;
    }


    /**
     * 插入定义
     */

    public  void insertMappings(Map<String, Object> params) {
        String id = GUID.newGUID();
        ColMappingDO colMappingDO = new ColMappingDO();
        colMappingDO.setId(id);
        colMappingDao.insert(colMappingDO);
    }

    /**
     * 删除定义
     * @param params
     */
    public  void delMappings(Map<String, Object> params){
        List<String> ids = (List<String>) params.get("ids");
        if (ids.size() > 0) {
            Example example = new Example(ColMappingDO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andIn("id", ids);
            colMappingDao.deleteByExample(example);
        }
    }

    /**
     * 更新定义
     * @param params
     */
    public void updateMappings(Map<String, Object> params){
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) params.get("data");
        if (dataList.size() > 0) {
            for (Map<String, Object> data : dataList) {

                ColMappingDO colMappingDO = new ColMappingDO();
                colMappingDO.setId(data.get("id").toString());
                if(data.get("tableId")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("tableId").toString())){
                        colMappingDO.setTableId((data.get("tableId").toString()));
                    }
                }
                if(data.get("excelColumn")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("excelColumn").toString())){
                        colMappingDO.setExcelColumn((data.get("excelColumn").toString()));
                    }
                }
                if(data.get("tableColumn")!=null) {
                    if(!StringUtil.isNullOrEmpty(data.get("tableColumn").toString())){
                        colMappingDO.setTableColumn((data.get("tableColumn").toString()));
                    }
                }
                colMappingDao.updateByPrimaryKey(colMappingDO);
            }
        }
    }

}
